/*
 * @Author: szx
 * @Date: 2022-03-17 14:01:17
 * @LastEditTime: 2022-03-17 14:12:55
 * @Description:
 * @FilePath: \leetcode\100-199\135\135_2.js
 */
/**
 * @param {number[]} ratings
 * @return {number}
 */
var candy = function (ratings) {
    const n = ratings.length;
    const res = new Array(n).fill(1);
    for (let i = 0; i < n - 1; i++) {
        if (ratings[i + 1] > ratings[i]) res[i + 1] = res[i] + 1;
    }
    for (let i = n - 1; i >= 1; i--) {
        if (ratings[i - 1] > ratings[i]) res[i - 1] = Math.max(res[i - 1], res[i] + 1);
    }
    return res.reduce((p, c) => p + c);
};
// console.log(candy([1, 2, 2]));
// console.log(candy([1, 3, 2, 2, 1]));
console.log(candy([1, 3, 4, 5, 2]));
